<?php

include( "./admin/include/func.php");
include( "./admin/include/thumb.php");

$panels = array ();

$g_id  = isset( $_GET['sorszam']) ? (int) $_GET['sorszam'] : 0;
$g_kod = isset( $_GET['kod']) ? trim( strip_tags( $_GET['kod'])) : '';

$db->sql = "SELECT lejarat 
            FROM hirdetes 
            WHERE id='$g_id' AND aktivkod='$g_kod' AND aktiv=1";
$db->query();

if (mysql_num_rows( $db->rs) < 1) {
	
	$error_panel = array (
		'caption' => 'Hiba',
		'title'   => 'Hiba!',
		'body'    => "<ul class='errors'>Nem létező vagy nem aktív hirdetés!</ul>",
		'height'  => 20
	);
	
	array_unshift( $panels, $error_panel);
		
} else {

	$rs = mysql_fetch_assoc( $db->rs);

	$p_lejarat = $rs['lejarat'];
	
	$panel = array (
		'caption' => 'Hirdetés hosszabítás',
		'title'   => 'Meghosszabíthatja a hirdetését!',
		'body'    => '',
		'height'  => '140'
	);

	array_push( $panels, $panel);

	if (isset($_POST['modositas'])) {

		$p_uj = (int)$_POST['uj'];

		$success = true;
		$errors = array();

		if (($p_uj) != '' && !preg_match('/^[0-9]{0,10}$/', $p_uj)) {
			$success = false;
			array_push($errors, "Hibás Lejárat!");
		}
		
		if ($p_uj < 1) {
			$success = false;
			array_push($errors, "A Lejárat nincs megadva!");
		}
    						  
		if (! $success) {
			
			$body    = "<ul class='errors'>";
			foreach ($errors as $err) $body .= "<li>$err</li>";
			$body   .= "</ul>";
			
			$error_panel = array (
				'caption' => 'Hiba',
				'title'   => 'Kérjük javítsa ki az alábbi hibákat!',
				'body'    => $body,
				'height'  => count( $errors) * 25 + 10
			);
			
			array_unshift( $panels, $error_panel);
			
		} else {			
			
			$db->sql = "UPDATE hirdetes 
						SET lastmodified=NOW(),lejarat=ADDDATE('$p_lejarat','$p_uj')
						WHERE id='$g_id' AND aktivkod='$g_kod'";					   
			$db->query();
			//print $db->sql;
			
			$panels = array ();
			
			$panel = array (
				'caption' => 'Információ',
				'title'   => 'Sikeres hosszabbítás!',
				'body'    => "<p class='success'>Hirdetését sikeresen meghosszabbította!<br /><a href='javascript:history.back()'>Vissza</a> &nbsp; | &nbsp; <a href='hirdetesek.php?sorszam=$g_id'>Hirdetés megtekintése</a></p>",
				'height'  => '50'
			);
			
			array_push( $panels, $panel);
		}
	}
}

?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
							
<div id="middle">

<?php foreach( $panels as $panel) { ?>
	
	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption"><?php print $panel['caption']; ?></p>
				<p class="hptitle"><?php print $panel['title']; ?></p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:<?php print $panel['height']; ?>px">
				<?php if ($panel['body'] != '') print $panel['body']; else { ?>
					
					<form name="form_hosszabbitas" id="form_hosszabbitas" method="post" enctype='multipart/form-data' accept-charset="utf-8">										
						
						<label for='id' class="required">Hirdetés sorszáma</label>
						<input name='id' type='text' disabled='true' value='<?php print $g_id; ?>' />
						<span class='note'><a href='hirdetesek.php?sorszam=<?php print $g_id; ?>' target='_blank'>Megtekintés &gt;&gt;</a></span>
						<br />
						
						<label for='lejarat'>Jelenlegi lejárat</label>
						<input name='lejarat' type='text' disabled='true' value='<?php print $p_lejarat; ?>' />
						<br />
						
						<label for='uj' class="required">Hosszabbítás</label>
						<select name='uj'>  
							<?php
								$db->sql = "SELECT id,lejarat_neve,lejarat_hossza FROM `lejarat` ORDER BY `order` DESC";
								$db->query();
								if (mysql_num_rows($db->rs) < 1) print "<option value='0'>Create a Lejárat first!</option>";
								while ($op = mysql_fetch_array($db->rs)) print "<option value='" . $op['lejarat_hossza'] . "'>" . $op['lejarat_neve'] . "</option>";
							?>                 
						</select>
						<p class='nnote'>Required</p>
						<br />
						
						<br />
											
						<label for="modositas">&nbsp;</label>
						<input name="modositas" type="submit" value="Módosítás"/>				

					</form>
				<?php } ?>           			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	<br />

<?php } ?>

<br />

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
